import { _decorator, Component, Node } from 'cc';
import { BaseManager } from '../../libs/rxjs/cc3/BaseManager';
import { color } from 'cc';
import { director } from 'cc';
import { sys } from 'cc';
import { jsb_sdk_m } from './JsbSDKManager';
import { System } from 'cc';
import { ReactiveProperty } from '../../libs/rxjs/cc3/ReactiveProperty';
const { ccclass, property } = _decorator;

@ccclass('CloudStorageSDKManager')
export class CloudStorageSDKManager extends BaseManager {

    public static instance: CloudStorageSDKManager;
    static ID = 'cloud_storage_sdk_system';
    get logColorOverride() { return color().fromHEX('#EECCDD') };

    static registerSystem() {
        director.unregisterSystem(director.getSystem(CloudStorageSDKManager.ID));
        director.registerSystem(CloudStorageSDKManager.ID, cloudstorage_sdk_m, System.Priority.MEDIUM);
        cloudstorage_sdk_m.init();
    }

    onInitManager() {
        this.log('onInitManager', this.timestamp);
        if (sys.isNative) {
            jsb_sdk_m.registerMsg('CloudStorageSDKManager.responseLoadGameData', this.responseLoadGameData.bind(this));
        }
    }

    gameData = ReactiveProperty.Create<any>(null);
    gameDataStr = ReactiveProperty.Create<string>(null);
    canSave = false;

    requestSaveGameData(data: string) {
        if (!sys.isNative) return;
        let obj = { data };
        jsb_sdk_m.sendMsg('CloudStorageSDK.requestSaveGameData', JSON.stringify(obj));
    }

    requestLoadGameData() {
        if (!sys.isNative) return;
        jsb_sdk_m.sendMsg('CloudStorageSDK.requestLoadGameData', '{}');
    }

    responseLoadGameData(param: string) {
        this.log('@responseLoadGameData', param);
        this.gameDataStr.value = JSON.parse(param)?.data;
        if (this.gameDataStr.value != null) {
            this.gameData.value = JSON.parse(this.gameDataStr.value);
        } else {
            this.gameData.value = null;
        }
        this.canSave = true;
    }

    requestDeleteGameData() {
        if (!sys.isNative) return;
        jsb_sdk_m.sendMsg('CloudStorageSDK.requestDeleteGameData', '{}');
    }

}


globalThis.CloudStorageSDKManager = CloudStorageSDKManager;

export const cloudstorage_sdk_m = CloudStorageSDKManager.instance = new CloudStorageSDKManager();